import Cookies from 'js-cookie';
// import { storageSession } from '@pureadmin/utils';
// import { useUserStoreHook } from '@/store/modules/user';
import { UserResult, LoginParam } from '@/api/user';
export interface DataInfo extends UserResult, LoginParam {
  /**用户登录账号,同时也是邮箱 */
  username: string;
}

export const sessionKey = 'trade_remember';
export const TokenKey = 'trade_token';
export const userEmail = 'trade_email';

/** 获取`token` */
// export function getToken() { }

/**
 * 设置存储token
 * @param data
 */
export function setToken(data: DataInfo) {
  const { access_token, expires_in, isRemember, username } = data;
  const expires = expires_in / 86400;
  Cookies.set(TokenKey, access_token, { expires });
  Cookies.set(userEmail, data.username, { expires });
  if (isRemember) {
    const remember = { username, isRemember };
    Cookies.set(sessionKey, JSON.stringify(remember), { expires });
  } else {
    Cookies.remove(sessionKey);
  }
}

// export function removeToken() { }

/** 格式化token（jwt格式） */
export const formatToken = (token: string): string => {
  return 'Bearer ' + token;
};
